// +build linux

package p2p

/*
#cgo CFLAGS: -I${SRCDIR}/include -g -Wall -O2 -std=gnu99
#cgo LDFLAGS: -L${SRCDIR}/lib/Linux/x64 -lP2PTunnelAPIs -lRDTAPIs -lIOTCAPIs -lTUTKGlobalAPIs -Wl,--start-group ${SRCDIR}/lib/Linux/x64/libcurl_tutk.a ${SRCDIR}/lib/Linux/x64/libjson-c_tutk.a ${SRCDIR}/lib/Linux/x64/libssl_tutk.a ${SRCDIR}/lib/Linux/x64/libcrypto_tutk.a -Wl,--end-group  -lpthread
#include <stdlib.h>
#include "TUTKGlobalAPIs.h"
#include "P2PTunnelAPIs.h"
#include "wrapApi.h"
 */
import "C"
import (
	"unsafe"
)

const (
	CallError = -1
	NoERROR = 0
)
func SetLicenseKey(key string)( int32 ){
	cKey := C.CString(key)
	defer C.free(unsafe.Pointer(cKey))
	result, _ := C.TUTK_SDK_Set_License_Key(cKey)
	return  int32(result)
}

func AgentInitialize(maxConn uint32) ( int32 ) {
	nMax := C.uint(maxConn)
	result, _ := C.P2PTunnelAgentInitialize(nMax)
	return  int32(result)
}

func AgentConnect(uid string) ( int32 )  {
	cUid := C.CString(uid)
	defer C.free(unsafe.Pointer(cUid))
	result,_ := C.P2PTunnelAgent_Connect_Wrap(cUid)
	return int32(result)
}
func AgentDisConnect(sid int32) ( int32 )  {
	result,_ := C.P2PTunnelAgent_Disconnect(C.int(sid))
	return int32(result)
}
func SetBufSize(sId int32,nSize int32)  ( int32 )  {
	result,_ := C.P2PTunnel_SetBufSize(C.int(sId), C.int(nSize))
	return  int32(result)
}
func PortMapping(sId int32,localPort uint16, remotePort uint16)  ( int32 )  {
	result,_ := C.P2PTunnelAgent_PortMapping(C.int(sId), C.ushort(localPort),C.ushort(remotePort))
	return int32(result)
}

func PortStopMapping(index int32) {
	if index >= 0{
		uIndex := uint32(index)
		C.P2PTunnelAgent_StopPortMapping(C.uint(uIndex))
	}
}
func LastIOTime(sid int32)  int32{
	res, _ := C.P2PTunnel_LastIOTime(C.int(sid))
	return int32(res)
}

